class_name Ui extends CanvasLayer

@onready var bg_color: ColorRect = $BgColor

@onready var game_over_container: VBoxContainer = $GameOverContainer
@onready var restart_button: Button = $GameOverContainer/RestartContainer/RestartButton
@onready var restart_container: VBoxContainer = $GameOverContainer/RestartContainer


@onready var point_container: MarginContainer = $PointContainer
@onready var start_container: VBoxContainer = $StartContainer
@onready var start_button: Button = $StartContainer/StartButton

@onready var select_charactor_container: VBoxContainer = $SelectCharactorContainer
@onready var charactor_list: HBoxContainer = $SelectCharactorContainer/AvatarPanelContainer/CharactorList
@onready var end_select_button: Button = $SelectCharactorContainer/EndSelectButton
@onready var avatar_panel_container: PanelContainer = $SelectCharactorContainer/AvatarPanelContainer
@onready var pause_container: VBoxContainer = $PauseContainer
@onready var back_button: Button = $PauseContainer/BackButton
@onready var point_rate_list: VBoxContainer = $GameOverContainer/PointRateList

@onready var player_info_list: HBoxContainer = $PointContainer/PlayerInfoList

var point_rate_res := preload("res://entity/ui/point_rate.tscn")
var player_info_item_res := preload("res://entity/ui/player_info_item.tscn")
var device_name := preload("res://entity/ui/device_name.tscn")
var charactor_container := preload("res://entity/ui/charactor_container.tscn")
## 生成角色选择框
func generate_charactor() -> void:
	for i in len(Game.player_data):
		var cha := charactor_container.instantiate()
		var avatar := cha.get_node("CharactorAvatar")
		avatar.self_modulate = Color(Game.player_data[i].modulate[0], Game.player_data[i].modulate[1], Game.player_data[i].modulate[2], 1)
		cha.name = "Cha" + str(i)
		cha.get_node("CharactorName").text = Game.player_data[i].name
		charactor_list.add_child(cha)

## 刷新头像上方控制器名称
func referesh_select_data() -> void:
	var child_arr := charactor_list.get_children()
	var values: Array = Game.player_id_map.values()
	for i in len(child_arr):
		var device_name_container: HBoxContainer = child_arr[i].get_node("DeviceNameContainer")
		if not i in values:
			NodeHelper.clear_children(device_name_container)
		else:
			var key: String = Game.player_id_map.find_key(i)
			if NodeHelper.find_child_by_prop(device_name_container, "text", Game.controller_arr[key]): continue
			var device_name_label := device_name.instantiate()
			device_name_label.text = Game.controller_arr[key]
			device_name_container.add_child(device_name_label)
				
		


func _ready() -> void:
	generate_charactor()
	Game.ui = self
	show_home()

## 每帧执行
func _process(_delta: float) -> void:
	update_point()

## 更新分数
func update_point() -> void:
	if Game.players.size():
		for p: Player in Game.players:
			if player_info_list.has_node(p.device): 
				var player_info_node: VBoxContainer = player_info_list.get_node(p.device)
				player_info_node.get_node("PointContainer/Point").text = str(p.point)
				player_info_node.get_node("SpeedContainer/Speed").text = str(p.speed)
			if point_rate_list.has_node(p.device): 
				var point_rate_node: Control = point_rate_list.get_node(p.device)
				point_rate_node.get_node("Point").text = str(p.point)


func init_point_label() -> void:
	if Game.players.size():
		for p: Player in Game.players:
			if not player_info_list.has_node(p.device):
				var modulate_data: Array = Game.player_data[p.charactor_id].modulate

				var player_info_node: VBoxContainer = player_info_item_res.instantiate()
				player_info_node.name = p.device
				player_info_node.get_node("DeviceNameContainer/Label").set("theme_override_colors/font_color", Color(modulate_data[0], modulate_data[1], modulate_data[2]))
				player_info_node.get_node("DeviceNameContainer/Label").text = Game.controller_arr[p.device]
				player_info_list.add_child(player_info_node)
				
			if not point_rate_list.has_node(p.device):
				var point_rate_node: Control = point_rate_res.instantiate()
				point_rate_node.name = p.device
				point_rate_node.get_node("Name").text = Game.controller_arr[p.device]
				point_rate_list.add_child(point_rate_node)


func show_home() -> void:
	NodeHelper.hide_children(self)
	start_container.show()
	bg_color.show()
	start_button.grab_focus()
	Game.current_state = Game.GameState.HOME


func show_pause_menu(is_pause: bool) -> void:
	if is_pause:
		pause_container.show()
		bg_color.show()
		back_button.grab_focus()
		Game.current_state = Game.GameState.MENU
	else:
		bg_color.hide()
		pause_container.hide()
		Game.current_state = Game.last_state


func show_gaming() -> void:
	NodeHelper.hide_children(self)
	init_point_label()
	point_container.show()
	bg_color.hide()
	Game.current_state = Game.GameState.GAMING


func show_game_over() -> void:
	NodeHelper.hide_children(self)
	restart_container.hide()
	game_over_container.show()
	bg_color.show()

	Game.current_state = Game.GameState.GAME_OVER
	await get_tree().create_timer(2).timeout
	restart_container.show()
	restart_button.grab_focus()

# 选择角色
func show_select_charactor() -> void:
	NodeHelper.hide_children(self)
	bg_color.show()
	select_charactor_container.show()
	avatar_panel_container.grab_focus()
	Game.current_state = Game.GameState.SELECT_CHARACTOR


func _on_start_button_button_down() -> void:
	SoundManager.play_ui("Click")
	Game.start_game()


func _on_restart_button_button_down() -> void:
	SoundManager.play_ui("Click")
	Game.restart_game()


func _on_exit_button_button_down() -> void:
	SoundManager.play_ui("Click")
	Game.exit_game()

func _on_select_charactor_button_button_down() -> void:
	SoundManager.play_ui("Click")
	show_select_charactor()



func _on_end_select_button_button_down() -> void:
	SoundManager.play_ui("Click")
	show_home()



func _on_avatar_panel_container_focus_entered() -> void:
	SoundManager.play_ui("Rollover")
	avatar_panel_container.get_theme_stylebox("panel").border_color = Color(1, 1, 1, 1)


func _on_avatar_panel_container_focus_exited() -> void:
	SoundManager.play_ui("Rollover")
	avatar_panel_container.get_theme_stylebox("panel").border_color = Color(1, 1, 1, 0)


func _on_back_button_button_down() -> void:
	Game.pause_game(false)
